<!DOCTYPE html>
<html>
<head>
    <title>添加药品--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }

    </style>
    <script type="text/javascript">
        //获取请求中参数的值
        String.prototype.GetValue= function(para) {
            let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
            let r = this.substr(this.indexOf("\?")+1).match(reg);
            if (r!=null) return unescape(r[2]); return null;
        }

        //根据id查询药品
        function queryById(id){
            $.ajax({
                type:"GET",
                url: "/drug/"+id, //RESTFul风格的API定义
                data: "",
                dataType:"json",
                success: function(vo){
                    let obj = vo.obj;
                    //填充更新前的信息
                    $("input[name='id']").val(obj.id);
                    $("input[name='inPrice']").val(obj.inPrice);
                    $("input[name='outPrice']").val(obj.outPrice);
                    $("input[name='drugName']").val(obj.drugName);
                    $("select[name='drugType']").val(obj.drugType);
                    $("input[name='description']").val(obj.description);
                    $("input[name='quality']").val(obj.quality);
                    $("textarea[name='detailDescription']").val(obj.detailDescription);
                    $("textarea[name='factory']").val(obj.factory);
                    $("input[name='instruction']").val(obj.instruction);
                    $("textarea[name='note']").val(obj.note);
                    if(obj.img!=null){
                        $("img[name='logo']").attr("src","/images/uploadFile/"+obj.img);
                    }else{
                        $("img[name='logo']").addClass("hidden");
                        $("img[name='logo']").parent().html("还没有上传药品图片");
                    }
                }
            });
        }

        $(function () {
            let url = document.location.toString();
            let id=url.GetValue("id");
            queryById(id);

            //保存按钮新增药品
            $("#updateMedicine").click(function(){
                $.ajax({
                    type:"POST",
                    url:"/drug/"+id,
                    data:$("#myForm").serialize()+"&_method=PUT",
                    dataType:"json",
                    success: function(vo){
                        if(vo.code==200) {
                            window.location.href = "/medicine/index.html";
                        }else{
                            alert("添加失败！"+vo.msg);
                        }
                    }
                });
            });

            $('#backList').click(function(){
                window.location.href="index.html";
            });

        });
    </script>
</head>
<body>
<form id="myForm" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">药品编号</td>
            <td><input type="text" id="id" name="id" disabled/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">图片</td>
            <td><img id="logo" name="logo" src="/images/uploadFile/"></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">进价</td>
            <td><input type="text" id="inPrice" name="inPrice" /></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">售价</td>
            <td><input type="text" id="outPrice" name="outPrice"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品名称</td>
            <td><input type="text" id="drugName" name="drugName" /></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">药品类型</td>
            <td>
                <label>
                    <select name="drugType">
                        <option value="">请选择药品类型</option>
                        <option value="中药">中药</option>
                        <option value="西药">西药</option>
                        <option value="处方">处方</option>
                        <option value="非处方">非处方</option>
                    </select>
                </label>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">简单描述</td>
            <td><input type="text" id="description" name="description"/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">保质期</td>
            <td><input type="text" id="quality" name="quality" value="36"/>月</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">详细描述</td>
            <td><textarea id="detailDescription" name="detailDescription"></textarea></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">生产厂商</td>
            <td><textarea id="factory" name="factory"></textarea></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">服用说明</td>
            <td><input type="text" id="instruction" name="instruction" value=""/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea id="note" name="note"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button id="updateMedicine" name="addMedicine" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;
                    <button id="backList" class="btn btn-success" type="button">返回列表</button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
</html>